package com.jushu.storbox.actions;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.FileType;
import com.jushu.storbox.service.ICloudDriveService;
import com.jushu.storbox.util.LogUtil;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.model.request.FolderBaseRequest;
import com.huawei.sharedrive.sdk.android.modelV2.request.FolderListRequestV2;
import com.huawei.sharedrive.sdk.android.modelV2.response.FileInfoResponseV2;
import com.huawei.sharedrive.sdk.android.modelV2.response.FolderListResponseV2;
import com.huawei.sharedrive.sdk.android.modelV2.response.FolderResponse;
import com.huawei.sharedrive.sdk.android.serviceV2.FileClientV2;
import com.huawei.sharedrive.sdk.android.serviceV2.FolderClientV2;

/**
 * move or copy class
 * 
 */
public class MoveAction extends Actions {
	
	private static final String TAG = "MoveAction";
	
	public MoveAction(Context context) {
		super(context);
	}

	public FolderListResponseV2 getFileList(
			ICloudDriveService service, FolderListRequestV2 request,
			Handler exceptionHandler) {
		LogUtil.i(TAG, "getFileList[Actions]");
		if (!checkToken(service, exceptionHandler)) {
			return null;
		}
		try {
			return FolderClientV2.getInstance().getFolderInfoList(request,
					ShareDriveApplication.getInstance().getAuthorization());
		} catch (ClientException e) {
			LogUtil.e(TAG, e.getCode() + "_" + e.getStatusCode());
			Message message = new Message();
			message.what = e.getStatusCode();
			message.obj = e;
			exceptionHandler.sendMessage(message);
			return null;
		}
	}
	
	public boolean checkFileFolderExist(
			ICloudDriveService service, FileFolderInfo fileFolderInfo,View item,
			Handler exceptionHandler) {
		LogUtil.i(TAG, "checkFileFolderExist[Actions]");
		if (!checkToken(service, exceptionHandler)) {
			return false;
		}
		try {
			if (FileType.File.value() == fileFolderInfo.getIsFile()){
				FileInfoResponseV2 serverItemInfo = FileClientV2
						.getInstance().getFileInfo(
						ShareDriveApplication.getInstance().getWnerID(),
						fileFolderInfo.getId(),
						ShareDriveApplication.getInstance().getAuthorization());
					if (serverItemInfo != null) 
					{
						return true;
					}
			}else{
				FolderBaseRequest request = new FolderBaseRequest();
		        request.setFolderID(fileFolderInfo.getId());
		        request.setAuthorization( ShareDriveApplication.getInstance().getAuthorization());
		        request.setOwnerID(ShareDriveApplication.getInstance().getWnerID());
				FolderResponse serverItemInfo = FolderClientV2.getInstance().getFolderInfo(request);
				if(serverItemInfo != null){
					return true;
				}
			}
		} catch (ClientException e) {
			LogUtil.e(TAG, e.getCode() + "_" + e.getStatusCode());
			Message message = new Message();
			message.what = e.getStatusCode();
			message.getData().putString("code", e.getCode());
			message.obj = item;
			exceptionHandler.sendMessage(message);
		}
		return false;
	}
}
